/**
 * 
 */
package rok3.projekt.zespver3.client.screens;

import static rok3.projekt.zespver3.client.Settings._window_height;
import static rok3.projekt.zespver3.client.Settings._window_width;
import rok3.projekt.zespver3.client.MyGame;
import rok3.projekt.zespver3.client.Resources;
import rok3.projekt.zespver3.client.Settings;
import rok3.projekt.zespver3.network.packet.CharacterState__;
import rok3.projekt.zespver3.network.packet.__StartGameInfo;
import rok3.projekt.zespver3.utils.GameClientUpdater;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

/**
 * @author sheepw76
 * 
 */
public class InGameScreen implements Screen {
	private MyGame game;
	private InGameStage stage;
	private GameClientUpdater updater;
	
	
	private short[] player_ids;
	
	public InGameScreen(MyGame myGame,__StartGameInfo sgi) {
		this.game = game;
		this.updater = Resources._updater;
		this.player_ids = sgi.player_ids;
		System.out.println("TestScreen#constructor - OK");
	}
	
	Label l = new Label("",Resources._skin);

	@Override
	public void render(float delta) {

		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		
		stage.catchInput(delta);
		
		if ( Settings._debug_on) l.setText("fps: " + Gdx.graphics.getFramesPerSecond());
		
		if( updater.isConnected() ) {
			updater.sendCharacterState(stage.getState());
			updater.sendEvents (stage.getEvents());
		}
		
		stage.act(delta);
		
		stage.draw();
		
		updater.applyStates();
	}

	@Override
	public void resize(int width, int height) {

	}

	@Override
	public void show() {
		stage = new InGameStage(player_ids,_window_width, _window_height, true);
		System.out.println("stage created");
		stage.addMainCharacterToStage();
		
		
		TextButton t = new TextButton("Debug ON/OFF",Resources._skin);
		t.addListener(new ClickListener() {
			public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
				Settings._debug_on = !Settings._debug_on;
			};
		} );
		t.setPosition(_window_width-140, _window_height - 50);
		l.setPosition(10, _window_height - 50);
		

		stage.addActor(l);
		stage.addActor(t);
		
		Gdx.input.setInputProcessor(stage);
		System.out.println("TestScreen#show - OK");
	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {
	//	Gdx.app.exit();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		Gdx.app.exit();
	}

	public InGameStage getStage() {
		return stage;
	}


}
